using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.TestDrivens;
using Pixysoft.Framework.Configurations.Core;
using Pixysoft.Framework.Configurations.Controller;

namespace Pixysoft.Framework.Configurations.TestCases
{
    [TestDriven(TestDrivenType.UnitTest, "ConfigurationController")]
    class UnitTest_021_ConfigurationController
    {
        static IAssert assert = Assert.New(true);

        [TestStart]
        public static bool TestCaseStart()
        {

            return true;
        }

        [TestStop]
        public static void TestCaseStop()
        {
        }

        [TestStateSave]
        public static ITestDrivenDocument TestCaseStateSave()
        {
            return assert.Document;
        }

        [TestCase("GetConfiguration")]
        public static void TestCase_000_GetConfiguration()
        {


            Assert.Call(UnitTest_020_ConfigurationSetting.TestCase_000_Save);

            IConfiguration setting = Assert.Cache.Load<IConfiguration>("1");

            string id = "NoebeConfiguration";
            string name = "name";

            assert.Write("create config first.");

            string path = StringFilter.RegularPath(AppDomain.CurrentDomain.BaseDirectory);
            string savefilename = path + StringHelper.default_config_filename;
            setting.Save(savefilename);

            IConfiguration config = ConfigurationController.Instance.GetConfiguration(savefilename, id, name);
            assert.IsNotNull(config);
            assert.IsEqual(id, config.Id);
        }

        [TestCase("GetConfiguration")]
        public static void TestCase_001_GetConfiguration()
        {


            assert.Write("create config first.");

            string id = "NoebeConfiguration";


            ConfigurationSetting setting = new ConfigurationSetting(id, null);
            string nodename = "nodename";
            IConfigurationNode node1 = new ConfigurationNode(nodename);
            node1.Text = "text";
            node1.Property.Add("property1", "property1");
            IConfigurationProperty property2 = node1.Property.Add("property2");

            string filename = StringFilter.RegularPath(AppDomain.CurrentDomain.BaseDirectory) + "Pixysoft.dll";
            string classname = "Pixysoft.ByteBuilder";
            node1.Property.Add(StringHelper.default_attribute_filename, filename);
            node1.Property.Add(StringHelper.default_attribute_classname, classname);
            setting.Node.Add(node1);

            IConfigurationNode node2 = new ConfigurationNode(nodename);
            setting.Nodes.Add(node2);

            string path = StringFilter.RegularPath(AppDomain.CurrentDomain.BaseDirectory);
            string savefilename = path + StringHelper.default_config_filename;
            setting.Save(savefilename);

            IConfiguration config = ConfigurationController.Instance.GetConfiguration(savefilename, id);
            assert.IsNotNull(config);
            assert.IsEqual(id, config.Id);

        }

        [TestCase("GetConfiguration")]
        public static void TestCase_002_GetConfiguration()
        {


            assert.Write("create config first.");

            string id = "NoebeConfiguration";


            ConfigurationSetting setting = new ConfigurationSetting(id, null);
            string nodename = "nodename";
            IConfigurationNode node1 = new ConfigurationNode(nodename);
            node1.Text = "text";
            node1.Property.Add("property1", "property1");
            IConfigurationProperty property2 = node1.Property.Add("property2");

            string filename = StringFilter.RegularPath(AppDomain.CurrentDomain.BaseDirectory) + "Pixysoft.dll";
            string classname = "Pixysoft.ByteBuilder";
            node1.Property.Add(StringHelper.default_attribute_filename, filename);
            node1.Property.Add(StringHelper.default_attribute_classname, classname);
            setting.Node.Add(node1);

            IConfigurationNode node2 = new ConfigurationNode(nodename);
            setting.Nodes.Add(node2);

            string path = StringFilter.RegularPath(AppDomain.CurrentDomain.BaseDirectory);
            string savefilename = path + StringHelper.default_config_filename;
            setting.Save(savefilename);

            foreach (IConfiguration config in ConfigurationController.Instance.GetConfiguration(savefilename))
            {
                assert.IsNotNull(config);
                assert.IsEqual(id, config.Id);
            }
        }

        [TestCase("DeserializeConfigurations")]
        public static void TestCase_003_DeserializeConfigurations()
        {


            assert.Write("create config first.");

            string id = "NoebeConfiguration";


            ConfigurationSetting setting = new ConfigurationSetting(id, null);
            string nodename = "nodename";
            IConfigurationNode node1 = new ConfigurationNode(nodename);
            node1.Text = "text";
            node1.Property.Add("property1", "property1");
            IConfigurationProperty property2 = node1.Property.Add("property2");

            string filename = StringFilter.RegularPath(AppDomain.CurrentDomain.BaseDirectory) + "Pixysoft.dll";
            string classname = "Pixysoft.ByteBuilder";
            node1.Property.Add(StringHelper.default_attribute_filename, filename);
            node1.Property.Add(StringHelper.default_attribute_classname, classname);
            setting.Node.Add(node1);

            IConfigurationNode node2 = new ConfigurationNode(nodename);
            setting.Nodes.Add(node2);

            string content = setting.Serialize();
            assert.Write(content);

            assert.IsEqual(1, ConfigurationController.Instance.DeserializeConfigurations(content).Count);
            foreach (IConfiguration config in ConfigurationController.Instance.DeserializeConfigurations(content))
            {
                assert.IsEqual(id, config.Id);
            }

            IConfiguration dconfig = ConfigurationController.Instance.DeserializeConfiguration(content, id, null);
            assert.IsEqual(id, dconfig.Id);
            assert.IsNull(dconfig.Name);
        }
    }
}
